package org.jeecg.modules.online.desform.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.online.desform.constant.PermissionTypeEnum;
import org.jeecg.modules.online.desform.entity.DesignFormAuth;
import org.jeecg.modules.online.desform.entity.DesignFormData;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 表单设计器字段权限表
 * @Author: jeecg-boot
 * @Date: 2019-09-03
 * @Version: V1.0
 */
public interface IDesignFormAuthService extends IService<DesignFormAuth> {

    /**
     * 根据 code 查询 auth
     *
     * @param desformCode
     * @param isGroup
     * @return
     */
    List<DesignFormAuth> queryByCode(String desformCode, boolean isGroup);

    /**
     * 根据 code 查询 auth
     */
    List<DesignFormAuth> queryByCode(String desformCode);

    /**
     * 根据 desformCode 和 authComKey 查询单个权限
     *
     * @param desformCode
     * @param authComKey
     * @return
     */
    List<DesignFormAuth> queryByCodeAndAuthComKey(String desformCode, String authComKey);

    /**
     * 根据 taskId 查询 auth
     *
     * @param desformCode
     * @param taskId
     * @return
     */
    List<DesignFormAuth> queryByTaskId(String desformCode, String taskId);

    /**
     * 修改多个权限控制
     *
     * @param auths
     * @return
     */
    boolean updateBatch(List<DesignFormAuth> auths);

    /**
     * 根据用户名获取用户信息以及拥有的部门、角色
     *
     * @param username
     * @return
     */
    JSONObject getUserInfoByUsername(String username);

    /**
     * 初始化某个字段的权限
     *
     * @param auth
     * @return
     */
    boolean initialAuthInfo(DesignFormAuth auth);

    /**
     * 检查 permissionType 是否合法
     *
     * @param permissionType
     * @return
     */
    boolean checkPermissionType(String permissionType);

    /**
     * 查询分页内容
     *
     * @param pageNo
     * @param pageSize
     * @param wrapper
     * @return
     */
    IPage<DesignFormAuth> pageList(Integer pageNo, Integer pageSize, Wrapper<DesignFormAuth> wrapper);

    /**
     * 根据 permissionType 新增
     *
     * @param auth
     * @param permissionType
     * @return
     */
    boolean addAuthByPermissionType(DesignFormAuth auth, String permissionType);

    /**
     * 根据 permissionType 修改
     *
     * @param auth
     * @param permissionType
     * @return
     */
    boolean editAuthByPermissionType(DesignFormAuth auth, String permissionType);

    /**
     * 查询按钮权限
     *
     * @param desformCode
     * @param username
     * @return
     */
    JSONObject queryButtonsAuth(String desformCode, String username);

    /**
     * 查询数据权限
     *
     * @param desformCode
     * @param username
     * @param superQueryList
     * @return
     */
    JSONArray queryDataAuth(String desformCode, String username, QueryWrapper<DesignFormData> queryWrapper, JSONArray superQueryList);

    /**
     * 根据不同的授权类型，查询不同的数据，做出不同的处理
     *
     * @param authScopeField 授权字段名称
     * @param authScopeCode  授权字段值
     * @param designFormAuth
     * @param permissionType
     * @param desformCode
     * @param request
     * @return
     */
    Result buildResultByPermissionType(String authScopeField, String authScopeCode, DesignFormAuth designFormAuth, String permissionType, String desformCode, HttpServletRequest request);

    /**
     * 数据权限、字段权限适用
     *
     * @param queryWrapper
     * @param authScopeField
     * @param authScopeCode
     * @return
     */
    Result buildDataFieldResult(QueryWrapper<DesignFormAuth> queryWrapper, String authScopeField, String authScopeCode, PermissionTypeEnum perType);

    /**
     * 构造返回值，仅按钮权限适用
     *
     * @param queryWrapper
     * @param authScopeField
     * @param authScopeCode
     * @return
     */
    Result buildButtonResult(QueryWrapper<DesignFormAuth> queryWrapper, String authScopeField, String authScopeCode);

    /**
     * 拼接逗号分隔形式存储的值所有可能出现的情况
     */
    QueryWrapper<DesignFormAuth> spliceAuthScopeWrapper(QueryWrapper<DesignFormAuth> i, String fieldName, String code);

}
